package org.mcjug.locationmailer.receiver;

import org.mcjug.locationmailer.R;
import org.mcjug.locationmailer.service.LocationService;
import org.mcjug.locationmailer.util.EmailUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class BatteryReceiver extends BroadcastReceiver {
    @Override
      public void onReceive(Context context, Intent intent) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        
        // Check if we should send location when battery is low
		String sendLocationWhenBatteryLowPrefName = context.getString(R.string.sendLocationWhenBatteryLowPreferenceName);
        boolean sendEmailWhenBatteryLow = prefs.getBoolean(sendLocationWhenBatteryLowPrefName, false);
        
        if (EmailUtil.canSendEmail(prefs) && sendEmailWhenBatteryLow) {
            context.startService(new Intent(context, LocationService.class));
        }
    }

}
